import { getJobGetjobtypes } from "@/api/siderBar/job";
import type { JobType } from "@/api/siderBar/model";
import type { Options } from "vue-request";

export interface CascadeJobTypesOption extends IOption<number>, JobType {
    children?: CascadeJobTypesOption[]
}
interface UseFetchJobTypesOpts extends Omit<Options<JobType[], []>, "cacheKey" | "staleTime" | "setCache" | "getCache"> {
}
const defCacheTime = 24 * 60 * 60 * 1000
export function useFetchJobTypes(opts?: UseFetchJobTypesOpts) {
    const { cacheTime = defCacheTime, ..._opts } = opts ?? {};
    const cacheKey = getLsKey('getJobGetjobtypes')
    const { data, loading, refreshAsync } = useRequest(() => getJobGetjobtypes(), {
        cacheKey,
        staleTime: cacheTime,
        setCache: (cacheKey, data) => {
            localStorage.setItem(cacheKey, JSON.stringify(data))
        },
        getCache: cacheKey => {
            const cached = localStorage.getItem(cacheKey)
            if (cached) {
                return JSON.parse(cached)
            }
        },
        ..._opts,
    })
    const cascadeJobTypes = computed(() => {
        const rootGroup: CascadeJobTypesOption[] = []
        const parentGroup: CascadeJobTypesOption[] = []
        const leafGroup: CascadeJobTypesOption[] = []
        if (data.value) {
            for (let i = 0; i < data.value.length; i++) {
                const element = data.value[i]
                if (element.level === 0) {
                    rootGroup.push({
                        ...element,
                        value: element.id,
                        label: element.name
                    })
                } else if (element.level === 1) {
                    parentGroup.push({
                        ...element,
                        value: element.id,
                        label: element.name
                    })
                } else {
                    leafGroup.push({
                        ...element,
                        value: element.id,
                        label: element.name
                    })
                }
            }
        }
        const options = rootGroup.reduce((prev, next) => {
            const option: CascadeJobTypesOption = {
                ...next,
                value: next.id,
                label: next.name
            }
            option.children = parentGroup.filter(l =>l.parentId === next.id).map(l => {
                const option: CascadeJobTypesOption = {
                    ...l,
                    value: l.id,
                    label: l.name
                }
                option.children = leafGroup.filter(m => m.parentId === l.id).map(m => {
                    return {
                        ...m,
                        value: m.id,
                        label: m.name,
                    }
                })
                return option
            })
            prev.push(option)
            return prev
        }, <CascadeJobTypesOption[]>[])
        return {
            rootGroup,
            parentGroup,
            leafGroup,
            options
        }
    })
    function clearCache() {
        localStorage.removeItem(cacheKey)
    }
    return {
        data,
        loading,
        refreshAsync,
        clearCache,
        cascadeJobTypes
    }
}